const {
    saveCommentInformSql,
    deleteCommentInformByUidSql,
    updateCommentInformByUidSql,
    queryCommentInformPageSql,
    queryAllCountCommentInformSql,
    queryCommentInformByInformTypeSql,
    queryCommentInformByUidSql,
    queryCommentInformAllSql,
} = require('../sql/commentInform')
const query = require('../utils/db')

const saveCommentInform = async (params) => {
    /**
     * 模式：$保存接口，参数接收$
     * 替换方式：提取出所有解析过的数据表字段，将连字符变量，替换为为小驼峰格式字符串
     */
    let {
        uid, informType, informReason, informPersonId,
        informCommentId, commentSource, sourceId, orderNum,
        createTime, updateTime
    } = params

    /**
     * 模式：$保存接口，sql参数接收$
     * 替换方式：提取出所有解析过的数据表字段，将连字符变量，替换为为小驼峰格式字符串
     */
    await query(
        saveCommentInformSql,
        [
            uid, informType, informReason, informPersonId,
            informCommentId, commentSource, sourceId, orderNum,
            createTime, updateTime
        ]
    )
    return true
}


const queryCommentInformPage = async (params) => {
    /**
     * 模式：$分页查询接口，参数接收$
     * 模式解析方式：提取解析过的数据表，默认值不为null的字段，将连字符变量，替换为为小驼峰格式字符串，排除 uid，新增 currentPage, pageSize
     */
    let {
        currentPage, pageSize,
        informType, informReason, informPersonId, informCommentId,
        commentSource, sourceId, orderNum
    }  = params

    let _currentPage = (currentPage - 1) * pageSize

    /**
     * 模式：$分页查询接口，sql参数接收$
     * 模式解析方式：提取解析过的数据表，默认值不为null的字段，将连字符变量，替换为为小驼峰格式字符串，排除 uid，新增 currentPage, pageSize
     */
    return await query(
        queryCommentInformPageSql,
        [
            `${informType}`,
            `%${informReason}%`,
            `%${informPersonId}%`,
            `%${informCommentId}%`,
            `${commentSource}`,
            `%${sourceId}%`,
            `${orderNum}`,
            _currentPage,
            pageSize
        ])
}
const queryAllCountCommentInform = async (params) => {
    /**
     * 模式：$查询接口，查询总条数参数接收$
     * 模式解析方式：提取解析过的数据表，默认值不为null的字段，将连字符变量，替换为为小驼峰格式字符串，排除 uid
     */
    let {
        informType, informReason, informPersonId, informCommentId,
        commentSource, sourceId, orderNum
    }  = params

    /**
     * 模式：$查询接口，sql查询总条数参数接收$
     * 模式解析方式：提取解析过的数据表，默认值不为null的字段，将连字符变量，替换为为小驼峰格式字符串，排除 uid
     */
    const result = await query(
        queryAllCountCommentInformSql,
        [
            `${informType}`,
            `%${informReason}%`,
            `%${informPersonId}%`,
            `%${informCommentId}%`,
            `${commentSource}`,
            `%${sourceId}%`,
            `${orderNum}`
        ])
    return result[0]
}


const updateCommentInformByUid = async (params) => {
    /**
     * 模式：$更新接口，参数接收$
     * 模式解析方式：提取出所有解析过的数据表字段，将连字符变量，替换为为小驼峰格式字符串，排除 uid createTime,updateTime
     */
    let {
        uid, informType, informReason, informPersonId,
        informCommentId, commentSource, sourceId, orderNum,
        createTime, updateTime
    } = params

    /**
     * 模式：$更新接口，sql参数接收$
     * 模式解析方式：提取出所有解析过的数据表字段，将连字符变量，替换为为小驼峰格式字符串，排除 uid createTime,updateTime
     */
    await query(
        updateCommentInformByUidSql,
        [
            informType, informReason, informPersonId, informCommentId,
            commentSource, sourceId, orderNum,
            createTime, updateTime, uid
        ]
    )
    return true
}

const deleteCommentInformByUid = async (uid) => {
    return await query(deleteCommentInformByUidSql, [uid])
}

/**
 * 模式 $查询接口 第二个参数大驼峰$ $查询接口 第二个参数小驼峰$
 *  替换方式：获取第二个参数，转换为大驼峰和小驼峰的格式
 */
const queryCommentInformByInformType = async (informType) => {
    const resultArr = await query(queryCommentInformByInformTypeSql, [informType])
    return resultArr[0]
}

const queryCommentInformByUid = async (uid) => {
    const resultArr = await query(queryCommentInformByUidSql, [uid])
    return resultArr[0]
}

const queryCommentInformAll = async () => {
    return await query(queryCommentInformAllSql, [])
}

module.exports = {
    saveCommentInform,
    deleteCommentInformByUid,
    updateCommentInformByUid,
    queryCommentInformPage,
    queryAllCountCommentInform,
    queryCommentInformByInformType,
    queryCommentInformByUid,
    queryCommentInformAll,
}
